import re
import requests
from multiprocessing import Pool
import os

def web_html(url):
    headers={"user-agent":'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    html=requests.get(url,headers).content.decode("gbk")
    return html

def contentDown(url):
    html=web_html(url)
    content1=re.findall('<td width="25%"><a href="(.*?)">(.*?)</a></td>',html)
    content2=re.findall('<td><a href="(.*?)">(.*?)</a></td>', html)
    content=content1+content2
    content_list=[]
    for i in range(len(content)):
        content_list.append("https://www.kanunu8.com/book3/6633/"+content[i][0])
    return content_list

def writeContent(url):
    html=web_html(url)
    title=re.findall('<a href="index.html">《(.*?)》</a>', html)[0]
    chapter=re.findall(r'< color="#dc143c" size="4">(.*?)</font>', html)[0].replace('&nbsp;&nbsp;', ' ')
    titles=re.findall('<p>(.*)</p>', html, re.S)[0].replace('<br />', '')
    with open(f"{title}\\{chapter}.txt", "w", encoding="utf-8") as f:
        f.write(titles)

def web(url):
    content_list = contentDown(url)
    html=web_html(content_list[0])
    title=re.findall('<a href="index.html">《(.*?)》</a>',html)[0]
    if not os.path.exists(title):
        os.makedirs(title)
    pool=Pool(5)
    pool.map(writeContent,content_list)

if __name__ == '__main__':
    web("https://www.kanunu8.com/book3/6633/index.html")
    print('OK')